<template>
  <div class="currentVersion">
    <rj-modal
      :title="$t('common.versionTIp', [versionInfo.version])"
      v-if="visible"
      :visible="true"
      :width="750"
      :footer="null"
      :closable="false"
    >
      <div class="releaseNotes">{{ versionInfo.releaseNotes }}</div>
      <div class="tr">
        <a
          target="_blank"
          :href="versionInfo.externalUrl"
          style="color: #2b6afd"
          >{{ $t('common.more2') }}</a
        >
      </div>
      <div class="checkHistory" @click="checkHistory">
        {{ $t('common.historyVers') }}
      </div>
      <div class="tr">
        <a-button type="primary" @click="onOk">{{
          $t('speedMultiOut.know')
        }}</a-button>
      </div>
    </rj-modal>
  </div>
</template>

<script>
import { requestHttp, maccApi } from '@utils/index';
import { RjModal } from '@components';

export default {
  name: 'currentVersion',
  components: {
    'rj-modal': RjModal,
  },
  data() {
    return {
      visible: false,
      versionInfo: [],
    };
  },
  created() {
    this.getVersionList();
  },
  methods: {
    // 获取版本信息
    async getVersionList() {
      let res = await requestHttp({
        api: `${maccApi.CLOUD_VERSIONLIST_GET_API.replace('{index}', 1).replace(
          '{size}',
          1
        )}`,
        method: 'GET',
      });
      if (res && res.code == 0) {
        if (res.list.length == 0) return;
        let releaseData = res.list[0] || {};
        let localVersion =
          window.localStorage && window.localStorage.getItem('CLOUD_VERSION');

        if (releaseData.version != localVersion) {
          this.versionInfo = res.list[0];
          this.visible = true;
        }

        if (process.env.VUE_APP_MACC_ENV == 'INTL') {
          if (location.href.includes('cloud-')) {
            this.versionInfo.externalUrl = `https://${location.host}/wiki/c/releaseLatest`;
          } else {
            this.versionInfo.externalUrl =
              'https://cloud-as.ruijienetworks.com/wiki/c/releaseLatest';
          }
        }
      }
      if (res.code !== 0) {
        this.$message.error(res.msg || this.$t('common.getNewVerFail'));
      }
      console.log(res, '版本信息---------');
    },
    // 查看历史版本
    checkHistory() {
      this.setLocalVersion();
      // this.$router.push({
      //   path: 'private_version_menu', //路径需修改?
      // });
      if (process.env.VUE_APP_MACC_ENV == 'INTL') {
        if (location.href.includes('cloud-')) {
          window.open(
            `https://${location.host}/wiki/article-historicalVersion-1`,
            '_blank'
          );
        } else {
          window.open(
            'https://cloud-as.ruijienetworks.com/wiki/article-historicalVersion-1',
            '_blank'
          );
        }
      } else {
        window.open('/help/release');
      }
    },
    onOk() {
      this.setLocalVersion();
      this.visible = false;
    },
    setLocalVersion() {
      window.localStorage &&
        window.localStorage.setItem('CLOUD_VERSION', this.versionInfo.version);
    },
  },
};
</script>

<style lang="less" scoped>
.releaseNotes {
  line-height: 2;
  max-height: 400px;
  overflow-y: auto;
}
.checkHistory {
  color: #2b6afd;
  margin: 10px 0;
  cursor: pointer;
}
.tr {
  text-align: right;
}
</style>
